package com.eeprint.system.service.terminal.impl;

import com.eeprint.common.utils.JSON;
import com.eeprint.system.domain.platform.PlatformTenant;
import com.eeprint.system.domain.common.PrintCodeData;
import com.eeprint.system.domain.terminal.dto.PrintJobData;
import com.eeprint.system.domain.terminal.vo.TerminalPrintJobData;
import com.eeprint.system.mapper.platform.PlatformTenantMapper;
import com.eeprint.system.mapper.tenant.TenantDocumentMapper;
import com.eeprint.system.mapper.tenant.TenantPrintJobMapper;
import com.eeprint.system.mapper.terminal.TerminalMapper;
import com.eeprint.system.service.terminal.TerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author Samven7
 * @date 2020/10/18 12:52
 */
@Service
public class TerminalServiceImpl implements TerminalService {

    @Autowired
    TerminalMapper terminalMapper;

    @Autowired
    TenantPrintJobMapper tenantPrintJobMapper;

    @Autowired
    TenantDocumentMapper tenantDocumentMapper;

    @Autowired
    PlatformTenantMapper platformTenantMapper;

    /**
     * 查询终端号是否存在（有效）
     *
     * @param tenantTerminalCode 终端登陆码
     * @return bool
     */
    @Override
    public boolean isExistTerminalCode(String tenantTerminalCode) {
        return platformTenantMapper.selectTenantByTerminalCode(tenantTerminalCode) != null;
    }

    /**
     * 获取所有打印记录
     *
     * @return 打印记录列表
     */
    @Override
    public List<TerminalPrintJobData> getAllPrintJobByTerminalCode(String tenantTerminalCode) {
        List<TerminalPrintJobData> result = null;
        // 根据终端登陆码获取租户
        PlatformTenant tenant = platformTenantMapper.selectTenantByTerminalCode(tenantTerminalCode);
        if(tenant != null){
            BigInteger id = tenant.getId();
            List<PrintJobData> data = terminalMapper.selectAllPrintJobByTenantID(id);
            try {
                result = new ArrayList<>();
                for (PrintJobData printJobData : data) {
                    PrintCodeData printCodeData = JSON.unmarshal(printJobData.getPrintCode(), PrintCodeData.class);
                    result.add(new TerminalPrintJobData(printJobData, printCodeData));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 打印记录移除
     *
     * @param id 作业编号
     * @return 结果
     */
    @Override
    public boolean removePrintJob(BigInteger id) {
        BigInteger documentID = tenantPrintJobMapper.selectPrintJobByID(id).getRelDocumentId();
        int a = tenantDocumentMapper.deleteDocumentByID(documentID);
        int b = tenantPrintJobMapper.deletePrintJobByID(id);
        return a > 0 && b > 0;
    }

    /**
     * 打印记录重打
     * @param tenantPrintJob 原打印记录
     * @return 结果
     */
    @Override
    public boolean ReprintPrintJob(BigInteger id) {
        tenantPrintJobMapper.insert(tenantPrintJobMapper.selectPrintJobByID(id));
        tenantPrintJobMapper.deletePrintJobByID(id);
        return false;
    }

    /**
     * 打印记录续打
     * @param id 作业编号
     * @return 结果
     */
    @Override
    public boolean continuePrintJob(BigInteger id) {
        return false;
    }


}
